package oa.hleast.xswl.controller.admin.activity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.pojo.domain.activity.*;
import oa.hleast.xswl.pojo.form.activity.CmsActivityCalendarForm;
import oa.hleast.xswl.pojo.form.activity.CmsCommonActivityForm;
import oa.hleast.xswl.pojo.vo.activity.CmsActivityCountVo;
import oa.hleast.xswl.pojo.vo.activity.ManagerUserSignUpVo;
import oa.hleast.xswl.pojo.vo.activity.ShowCommonActivityVo;
import oa.hleast.xswl.pojo.vo.activity.ShowUserSignInVo;
import oa.hleast.xswl.service.activity.*;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

@Api(tags = "【系统管理】 通用活动报名")
@RestController("ActivityCommendController")
@RequestMapping("/api.admin/v1/activity/commend")
@Slf4j
@AllArgsConstructor
public class ActivityCommendController {

    private final ICmsCommonActivityService commonActivityService;
    private final ICmsActivityCalendarService calendarService;
    private final ICmsActivityTemplateService templateService;
    private final ICmsActivityAttributesService attributesService;
    private final ICmsUserSignUpService userSignUpService;

    @ApiOperation(value = "活动列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "activityTypeId", value = "活动类型ID", paramType = "query", dataType = "Long",dataTypeClass = Long.class)

    })
    @GetMapping("/pageList")
    public Result list(String queryMode, Integer page, Integer limit, Long activityTypeId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<CmsCommonActivity> result = commonActivityService.pageList(page, limit, activityTypeId, null);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    /**
     * 活动详情
     */
    @ApiOperation(value = "活动详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("detail/{id}")
    public Result detail(@PathVariable @NotNull Long id) {

        ShowCommonActivityVo vo = commonActivityService.detail(id, null);
        return Result.success(vo);
    }

    /**
     * 新增活动
     */
    @ApiOperation(value = "新增活动", httpMethod = "POST")
//    @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsCommonActivityForm", dataTypeClass = CmsCommonActivityForm.class)
    @PostMapping("/add")
    public Result add(@RequestBody CmsCommonActivityForm form) {
        Boolean result = commonActivityService.add(form);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改活动", httpMethod = "PUT")
    @PutMapping(value = "/update/{id}")
    public Result update(
            @RequestBody @NotNull CmsCommonActivityForm form) {

        Boolean result = commonActivityService.update(form);
        return Result.judge(result);
    }

    /**
     * 删除活动
     */
    @ApiOperation(value = "删除活动", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {

        Boolean result = commonActivityService.deleteById(id);
        return Result.judge(result);
    }

    /**
     * 某活动某场此报名签到情况列表分页
     */
    @ApiOperation(value = "某活动某场此报名签到情况列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "activityId", value = "活动ID", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/calendar/signInPageList")
    public Result calendarList(@Valid @RequestParam @NotNull String queryMode,
                               @Valid @RequestParam @NotNull Integer page,
                               @Valid @RequestParam @NotNull Integer limit,
                               @Valid @RequestParam @NotNull Long activityId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<CmsActivityCountVo> result = calendarService.signInPageList(page, limit, activityId, null);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    @ApiOperation(value = "某活动某月的日期", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "month", value = "月", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "activityId", value = "活动id", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/findCalendarAfterToday")
    public Result findActivityByTime(@Valid @RequestParam @NotNull Long activityId,
                                     @Valid @RequestParam @NotNull Integer year,
                                     @Valid @RequestParam @NotNull Integer month) {
        List<CmsActivityCalendar> voList = commonActivityService.findActivityByTime(activityId, year, month);
        return Result.success(voList);
    }

    @ApiOperation(value = "活动日历详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/calendar/detail/{id}")
    public Result calendarDetail(@PathVariable @NotNull Long id) {
        CmsActivityCalendar vo = calendarService.detail(id);
        return Result.success(vo);
    }

    @ApiOperation(value = "新增活动日历", httpMethod = "POST")
    @PostMapping("/calendar/add")
    public Result add(@Valid @RequestBody CmsActivityCalendarForm form) {
        Boolean result = calendarService.add(form);
        return Result.judge(result);
    }


    /**
     * 修改活动日历
     */
    @ApiOperation(value = "修改活动日历", httpMethod = "PUT")
    @ApiImplicitParam(name = "form", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsActivityCalendar",dataTypeClass = CmsActivityCalendar.class)
    @PutMapping(value = "/calendar/update")
    public Result update(@RequestBody @NotNull CmsActivityCalendar form) {

        Boolean result = calendarService.update(form);
        return Result.judge(result);
    }

    /**
     * 删除活动日历
     */
    @ApiOperation(value = "删除活动日历", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("/calendar/{ids}")
    public Result calendarDelete(@PathVariable("ids") String ids) {

        Boolean result = calendarService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(result);
    }

    //活动报名模板
    @ApiOperation(value = "报名模板列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/template/pageList")
    public Result templateList(String queryMode, Integer page, Integer limit) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<CmsActivityTemplate> result = templateService.pageList(page, limit, null, null);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    @ApiOperation(value = "报名模板详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/template/detail/{id}")
    public Result templateDetail(@PathVariable @NotNull Long id) {
        CmsActivityTemplate vo = templateService.detail(id);
        return Result.success(vo);
    }

    @ApiOperation(value = "新增报名模板", httpMethod = "POST")
    @PostMapping("/template/add")
    public Result templateAdd(@RequestBody CmsActivityTemplate form) {

        Boolean result = templateService.add(form);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改报名模板", httpMethod = "PUT")
    @PutMapping(value = "/template/update")
    public Result templateUpdate(
            @RequestBody @NotNull CmsActivityTemplate form) {

        Boolean result = templateService.update(form);
        return Result.judge(result);
    }

    @ApiOperation(value = "删除报名模板", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("/template/{ids}")
    public Result templateDelete(@PathVariable("ids") String ids) {

        Boolean result = templateService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(result);
    }

    @ApiOperation(value = "根据类型获取属性列表", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/findAttributeByTypeId/{id}")
    public Result findAttributeByTypeId(@PathVariable @NotNull Long id) {
        List<CmsActivityAttributes> voList = attributesService.findAttributeByTypeId(id);
        return Result.success(voList);
    }

    @ApiOperation(value = "根据活动ID获取报名模板", httpMethod = "GET")
    @ApiImplicitParam(name = "activityId", value = "活动id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/getTemplateByActivityId")
    public Result getTemplateByActivityId(@RequestParam @NotNull Long activityId) {
        CmsActivityTemplate vo = templateService.getTemplateByActivityId(activityId);
        return Result.success(vo);
    }



    /**
     * 报名某场次/某节课的所有人员
     */
    @ApiOperation(value = "报名某场次/某节课的所有人员", httpMethod = "GET")
    @ApiImplicitParam(name = "calendarId", value = "某场次/某节课/某日历ID", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/findUserByActivityId")
    public Result findUserByActivityId(@RequestParam @NotNull Long calendarId) {
        List<CmsUserSignUp> voList = userSignUpService.findUserByCalendarId(calendarId);
        return Result.success(voList);
    }

    /**
     * 活动报名审核
     * @param scenesId 用户报名活动的ID
     * @param reviewStatus 审核状态 0未审核 1审核通过 2审核不通过
     * @return 是否完成
     */
    @ApiOperation(value = "活动报名审核", httpMethod = "PUT")
    @GetMapping(value = "/activityReview")
    public Result activityReview(@RequestParam Long scenesId,@RequestParam String reviewStatus,@RequestParam Long userId) {

        Boolean result = userSignUpService.activityReview(scenesId,reviewStatus,userId);
        return Result.judge(result);
    }

    /**
     * 取消活动报名
     * @param scenesId 用户报名活动的ID
     * @return 是否完成
     */
    @ApiOperation(value = "取消活动报名", httpMethod = "PUT")
    @GetMapping(value = "/activityCancel")
    public Result activityCancel(@RequestParam Long scenesId,@RequestParam Long userId) {

        Boolean result = userSignUpService.activityCancel(scenesId,userId);
        return Result.judge(result);
    }

    /**
     * 某活动的报名情况
     */
    @ApiOperation(value = "某活动的报名情况", httpMethod = "GET")
    @ApiImplicitParam(name = "activityId", value = "活动ID", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/findUserSignUpByForActivityId")
    public Result findUserSignUpByForActivityId(@RequestParam @NotNull Long activityId,
                                                @RequestParam @NotNull Integer page,
                                                @RequestParam @NotNull Integer limit) {
        Page<ManagerUserSignUpVo> result = userSignUpService.findUserSignUpByForActivityId(activityId, page, limit);
        return Result.success(result.getRecords(), result.getTotal());
    }



    /**
     * 报名某场次/某节课的所有人员的签到情况
     */
    @ApiOperation(value = "报名某场次/某节课的所有人员的签到情况", httpMethod = "GET")
    @ApiImplicitParam(name = "calendarId", value = "某场次/某节课/某日历ID", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/findUserSignInByForCalendar")
    public Result findUserSignInByForCalendar(@RequestParam @NotNull Long calendarId) {
        List<ShowUserSignInVo> voList = userSignUpService.findUserSignInByForCalendar(calendarId);
        return Result.success(voList);
    }

    /**
     * 更改用户体温
     */
    @ApiOperation(value = "更改用户体温", httpMethod = "GET")
    @ApiImplicitParam(name = "signInId", value = "用户报名数据ID", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/updateBodyTemperature")
    public Result updateBodyTemperature(@RequestParam @NotNull Long signInId, @RequestParam @NotNull String bodyTemperature) {
        Boolean result = userSignUpService.updateBodyTemperature(signInId, bodyTemperature);
        return Result.judge(result);
    }









}
